package meituan;

import java.util.Scanner;

/**
 * @author le
 */
public class Meituan2
{
    public static void main(String[] args)
    {
        int[] a = {1, 1, 1, 4, 4, 5};
        int[] b = {0, 0, 0, 3, 3, 4};

        // n = 6, m = 8, (a + x) mod m
        boolean[] used = new boolean[6];

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[] x = new int[n];
        int[] y = new int[n];
        for (int i = 0;i < n;i++){
            x[i] = in.nextInt();
        }
        for (int i = 0;i < n;i++){
            y[i] = in.nextInt();
        }
        int count  = 1;
        while(count <= n){
            int res = count*m+y[n-1]-x[n-1];
            int index = 0;
            for (int i = 0;i < n;i++){
                if ((x[i]+res)%m == y[i]){
                    index++;
                }
            }
            if (index == n){
                System.out.println(res);
                break;
            }
            index = 0;
            count++;

        }



        in.close();

        System.out.println(used[0]);




    }
}
